package top.xmj.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import top.xmj.config.interceptor.UserLoginInterceptor;


/**
 *
 * @author 冰咖啡
 * @Date:2/12/2022 下午 9:36
 * @Description: 添加拦截器
 *
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {


    /**
     * 图片保存路径，自动从yml文件中获取数据
     *   示例： E:/images/
     */
    @Value("${file-save-path}")
    private String fileSavePath;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        /**
         * 配置资源映射
         * 意思是：如果访问的资源路径是以“/images/”开头的，
         * 就给我映射到本机的“E:/images/”这个文件夹内，去找你要的资源
         * 注意：E:/images/ 后面的 “/”一定要带上
         */
        registry.addResourceHandler("/images/**")
                .addResourceLocations("file:"+fileSavePath);
    }

    /**
     * 注册自定义拦截器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new UserLoginInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/user/login",  //登录界面
                        "/user/info",
                        "/**/webjars/**",
                        "/user/register",
                        "/user/logout",
                        "/websocket/**",
                        "/**/*swagger*/**",
                        "/**/*.ico*",
                        "/mail/**",
                        "/**/images/**",
                        "/**/uploadFile/**",
                        "/**/*.html",                //html静态资源
                        "/**/*.js",                  //js静态资源
                        "/**/*.css"                  //css静态资源
                );//开放登录路径
    }

}